/*
 This is a fork on 'Stylesheet' library due to Anton Samoylov (http://mifjs.net)
 license under MIT-Style License (http://mifjs.net/license.txt) requiring
 MooTools Core 1.3.*.
*/
Stylesheet = new Class({

  version: '0.1',

  initialize: function(properties)
  {
    this.createSheet(properties);
    this.rules = {};
    this.styles = {};
    this.index = [];
    this.temp = new Element('div');
  },

  createSheet: function(properties)
  {
    var style = new Element('style', properties).inject(document.head);
    this.sheet = style.styleSheet || style.sheet;
  },

  addRule: function(selector, styles)
  {
    selector = selector.trim();
    if (selector.contains(',')) {
      var selectors = selector.split(',');
      selectors.each(
        function(selector) { this.addRule(selector, styles) }, this);
      return this;
    }
    var styles = (typeOf(styles) == 'string') ? styles : this.stylesToString(styles);
    if (!styles) return;
    var sheet = this.sheet;
    if (sheet.addRule) {
      sheet.addRule(selector, styles);
    } else {
      sheet.insertRule([selector, '{', styles, '}'].join(''), sheet.cssRules.length);
    }
    var rules = this.getRules();
    this.rules[selector] = rules.getLast();
    this.styles[selector] = styles;
    this.index.push(selector);
    return this;
  },

  addRules: function(rules)
  {
    for (selector in rules) {
      this.addRule(selector, rules[selector]);
    }
    return this;
  },

  stylesToString: function(styles)
  {
    this.temp.setStyles(styles);
    var string = this.temp.style.cssText;
    this.temp.style.cssText = '';
    return string;
  },

  removeRule: function(index)
  {
    var sheet = this.sheet;
    if (typeOf(index) == 'string') {
      var selector = index.trim();
      if (selector.contains(',')) {
        var selectors = selector.split(',');
        selectors.each(
          function(selector) { this.removeRule(selector) }, this);
        return this;
      }
      var index = this.getRules().indexOf(this.getRule(selector));
      if (index < 0) return this;
    }
    sheet.removeRule ? sheet.removeRule(index) : sheet.deleteRule(index);
    var selector = this.index[index];
    this.index.erase(selector);
    delete this.rules[selector];
    delete this.styles[selector];
    return this;
  },

  getRule: function(selector)
  {
    return (typeOf(selector) == 'string') ? this.rules[selector] : this.getRules()[selector];
  },

  getRules: function()
  {
    return Array.from(this.sheet.cssRules || this.sheet.rules);
  }

});